package com.ggkt.algorithm;

import java.util.Arrays;

public class BubbleTest {
    public static void main(String[] args) {
        int array[] = new int[]{10,11,1,22,2};
        System.out.println(Arrays.toString(array));
        BubbleTest.bubbleSort(array);
        System.out.println(Arrays.toString(array));
    }

    public static void bubbleSort(int[] arr){
        int tmp;
        for(int i=0; i<arr.length-1;i++){
            //arr.length=5,i=0,循环4次
            for(int j=0;j<arr.length-i-1;j++){
                //arr[0]>arr[1]?,a[1]>[a2]?,a[2]>[a3]?,a[3]>[a4]?
                if(arr[j]>arr[j+1]){
                    tmp=arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=tmp;
                }
            }
            System.out.println("第"+i+"次，arrar is:"+Arrays.toString(arr));
        }
    }
}
